// pages/cate/cate.js
import http from "../../api/api";
Page({
    /**
     * 页面的初始数据
     */
    data: {
        /**
         * 轮播图数据
         */
        swiperList: [{
                image_src: "https://api-hmugo-web.itheima.net/pyg/banner1.png",
                open_type: "navigate",
                goods_id: 129,
                navigator_url: "/pages/goods_detail/index?goods_id=129",
            },
            {
                image_src: "https://api-hmugo-web.itheima.net/pyg/banner1.png",
                open_type: "navigate",
                goods_id: 129,
                navigator_url: "/pages/goods_detail/index?goods_id=129",
            },
        ],
        /**
         * 导航栏数据
         */
        catList: [{
                name: "分类",
                image_src: "https://api-hmugo-web.itheima.net/pyg/icon_index_nav_4@2x.png",
                open_type: "switchTab",
                navigator_url: "/pages/category/index",
            },
            {
                name: "分类",
                image_src: "https://api-hmugo-web.itheima.net/pyg/icon_index_nav_4@2x.png",
                open_type: "switchTab",
                navigator_url: "/pages/category/index",
            },
            {
                name: "分类",
                image_src: "https://api-hmugo-web.itheima.net/pyg/icon_index_nav_4@2x.png",
                open_type: "switchTab",
                navigator_url: "/pages/category/index",
            },
            {
                name: "分类",
                image_src: "https://api-hmugo-web.itheima.net/pyg/icon_index_nav_4@2x.png",
                open_type: "switchTab",
                navigator_url: "/pages/category/index",
            },
        ],
        // 楼层数据
        floorList: [],
        // 是否显示返回顶部
        isShowTop: false
    },
    /**
     * 获取轮播图数据
     */
    async getSwiperData() {
        try {
            const {
                message
            } = await http.getSwiperData();
            this.setData({
                swiperList: message,
            });
        } catch (error) {}
    },

    /**
     * 获取导航栏数据
     */
    async getCatItems() {
        try {
            const {
                message
            } = await http.getCatItems();
            this.setData({
                catList: message,
            });
        } catch (error) {}
    },
    /**
     * 菜单点击事件
     */
    goCat(e) {
        try {
            const index = e.currentTarget.dataset.index;
            if (index === 0) {
                wx.switchTab({
                    url: "../cate/cate",
                });
            }
        } catch (error) {}
    },

    /**
     * 获取楼层数据
     */
    async getFloorList() {
        try {
            const {
                message
            } = await http.getFloorList();
            this.setData({
                floorList: message,
            })
        } catch (error) {}
    },

    /**
     * 返回顶部
     */
    goTop() {
        // 平滑返回顶部
        wx.pageScrollTo({
            scrollTop: 0,
            duration: 1000
        })
    },

    /**
     * 滚动大于可视区域显示返回顶部按钮
     */
    onPageScroll(e) {
        const info = wx.getSystemInfoSync()
        if (e.scrollTop > info.screenHeight) {
            this.setData({
                isShowTop: true
            })
        } else {
            this.setData({
                isShowTop: false
            })
        }
    },

    /**
     * 跳转搜索页
     */
    goSearch() {
        wx.navigateTo({
            url: '../search/search',
        })
    },

    // 跳转商品列表页
    goGoods(e) {
        try {
            const query = e.currentTarget.dataset.query.split('=')[1];
            wx.navigateTo({
                url: '../goods/goods?query=' + query,
            })
        } catch (error) {

        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getSwiperData();
        this.getCatItems()
        this.getFloorList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {},
});